Imports System
Imports System.Configuration
Imports MyClasses
Imports DAL
Imports System.Drawing.Printing
Public Class frmDocuments
    Dim tableFont, titlefont, headfont, tabletitlefont As Font

    Dim intLayoutId As Integer

    Dim pageWidth As Integer
    Dim Cdoc As New CDocument
    ''
    Dim curY As Integer
    Dim X1, X2 As Integer
    ' Dim W1, W2, W3 As Integer
    Dim wi As Integer
    Dim PHi As Integer
    Dim Y As Integer
    'Dim ChkPageSize As Double
    Dim Foot1 As String
    Dim Foot2 As String
    Dim Foot3 As String


#Region "User Define"
    Public Sub SetValues()
        Try
            With Cdoc
                .Height = CDbl(checkTextBox(txtHeight.Text))
                .Width = CDbl(checkTextBox(txtWidth.Text))
                .TopMargin = CDbl(checkTextBox(txtTopM.Text))
                .BottomMargin = CDbl(checkTextBox(txtBottomM.Text))
                .LeftMargin = CDbl(checkTextBox(txtLeftM.Text))
                .RightMargin = CDbl(checkTextBox(txtRightM.Text))
                .LayoutLabel = txtLaylbl.Text
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetValues")
        End Try
    End Sub

    Public Function ValidateData() As Boolean
        Try
            'Divya@15/01/2008 : Added following Validation
            Dim count As Integer
            Dim dtGetData As DataTable = BDocument.GetData(Cdoc.LayoutId)
            If btnSetValue.Text = "&Save" Then
                For count = 0 To dtGetData.Rows.Count - 1
                    If Trim(txtLaylbl.Text) = dtGetData.Rows(count).Item("Layoutlbl") Then
                        BCommon.ShowMsgError(10331)
                        txtLaylbl.Focus()
                        Return False
                    Else
                        Return True
                    End If
                Next
            End If
            '>>End

            If Trim(txtLaylbl.Text) = "" Then
                BCommon.ShowMsgError(10297)
                txtLaylbl.Focus()
                Return False
            ElseIf CDbl(Trim(txtHeight.Text)) < 1 Or Trim(txtHeight.Text) = "" Then
                BCommon.ShowMsgError(10298)
                txtHeight.Focus()
                Return False
            ElseIf CDbl(Trim(txtTopM.Text)) > 1 Then
                BCommon.ShowMsgError(10300)
                txtTopM.Focus()
                Return False
            ElseIf CDbl(Trim(txtBottomM.Text)) > 1 Then
                BCommon.ShowMsgError(10302)
                txtBottomM.Focus()
                Return False
            ElseIf CDbl(Trim(txtWidth.Text)) < 1 Or Trim(txtWidth.Text) = "" Then
                BCommon.ShowMsgError(10299)
                txtWidth.Focus()
                Return False
            ElseIf CDbl(Trim(txtLeftM.Text)) > 1 Then
                BCommon.ShowMsgError(10301)
                txtLeftM.Focus()
                Return False
            ElseIf CDbl(Trim(txtRightM.Text)) > 1 Then
                BCommon.ShowMsgError(10303)
                txtRightM.Focus()
                Return False
            Else
                Return True
            End If

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ValidateData")
        End Try
    End Function

    Public Sub PrintBill()

        'Dim PHi As Double
        Dim PWi As Integer
        Dim Ps As PaperSize
        Try
            Dim dtGetData As DataTable = BDocument.GetData(Cdoc.LayoutId)
            With PrintDoc.DefaultPageSettings
                PHi = (CInt(dtGetData.Rows(0).Item("Height")) * 100)
                PWi = (CInt(dtGetData.Rows(0).Item("Width")) * 100)
                Ps = New PaperSize("Cust", PWi, PHi)

                .Margins.Top = (CInt(dtGetData.Rows(0).Item("TopM")) * 100)
                .Margins.Bottom = (CInt(dtGetData.Rows(0).Item("BottomM")) * 100)
                .Margins.Left = (CInt(dtGetData.Rows(0).Item("LeftM")) * 100)
                .Margins.Right = (CInt(dtGetData.Rows(0).Item("RightM")) * 100)
                .PaperSize = Ps

                'ChkPageSize = PHi - (325 + .Margins.Bottom)
            End With

            headfont = New Font("MS Serif", 12, FontStyle.Bold)
            tableFont = New Font("MS Serif", 8)
            titlefont = New Font("MS Serif", 9)

            X1 = PrintDoc.DefaultPageSettings.Margins.Left

            With PrintDoc.DefaultPageSettings
                pageWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
            End With

            X2 = X1 + 120
            'X3 = CInt(X2 + pageWidth * 0.5)
            ' W1 = X2 - X1
            ' W2 = X3 - X2
            ' W3 = pageWidth - X3

            'COLLECT THE FOOTER LINES 
            Foot1 = BCommon.GetPrefValue("SALEFOOT1")
            Foot2 = BCommon.GetPrefValue("SALEFOOT2")
            Foot3 = BCommon.GetPrefValue("SALEFOOT3")

            PrintPrevieD.Document = PrintDoc
            'Divya @ 14/03/2008
            PrintPrevieD.ShowIcon = True
            Me.PrintPrevieD.Icon = Global.Pharmacy.My.Resources.RxOffice1Icon32x32
            '>>End
            PrintPrevieD.ShowDialog()

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "PrintBill")
        End Try
    End Sub
    'Divya 17/01/2008
    Public Sub Preview()
        Dim PWi As Integer
        Dim Ps As PaperSize

        Try
                With PrintDoc.DefaultPageSettings
                PHi = CInt(CDbl(txtHeight.Text) * 100)
                PWi = CInt(CDbl(txtWidth.Text) * 100)
                Ps = New PaperSize("Cust", PWi, PHi)

                .Margins.Top = txtTopM.Text * 100
                .Margins.Bottom = txtBottomM.Text * 100
                .Margins.Left = txtLeftM.Text * 100
                .Margins.Right = txtRightM.Text * 100
                .PaperSize = Ps

                'ChkPageSize = PHi - (325 + .Margins.Bottom)
            End With

            headfont = New Font("MS Serif", 12, FontStyle.Bold)
            tableFont = New Font("MS Serif", 8)
            titlefont = New Font("MS Serif", 9)
            tabletitlefont = New Font("MS Serif", 8, FontStyle.Bold)

            X1 = PrintDoc.DefaultPageSettings.Margins.Left

            With PrintDoc.DefaultPageSettings
                pageWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
            End With
            'PrintDoc.OriginAtMargins = True
            With PrintDoc.DefaultPageSettings
                pageWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
            End With

            X2 = X1 + 1204
            'X3 = X2 + pageWidth * 0.5
            ' W1 = X2 - X1
            ' W2 = X3 - X2
            'W3 = pageWidth - X3

            Foot1 = BCommon.GetPrefValue("SALEFOOT1")
            Foot2 = BCommon.GetPrefValue("SALEFOOT2")
            Foot3 = BCommon.GetPrefValue("SALEFOOT3")


            PrintPrevieD.Document = PrintDoc
            'Divya @ 14/03/2008
            PrintPrevieD.ShowIcon = True
            Me.PrintPrevieD.Icon = Global.Pharmacy.My.Resources.RxOffice1Icon32x32
            '>>End
            PrintPrevieD.ShowDialog()
           
            'PrintDoc.Print()

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ShowCustomerDetails")
        End Try
    End Sub
    Private Function checkTextBox(ByVal txtBox As String)
        Try
            If txtBox = "" Then
                Return Format(0)
            Else
                Return Format(CType(txtBox, Double))
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "checkTextBox")
            Return Nothing
        End Try
    End Function
    Public Sub ClearData()
        Try
            txtHeight.Text = Format(0)
            txtHeight.Tag = Format(0)
            txtWidth.Text = Format(0)
            txtWidth.Tag = Format(0)
            txtTopM.Text = Format(0)
            txtTopM.Tag = Format(0)
            txtBottomM.Text = Format(0)
            txtBottomM.Tag = Format(0)
            txtLeftM.Text = Format(0)
            txtLeftM.Tag = Format(0)
            txtRightM.Text = Format(0)
            txtRightM.Tag = Format(0)

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ClearData")
        End Try
    End Sub

    Public Sub CancelData()
        Try
            txtHeight.Text = CStr(txtHeight.Tag)
            txtWidth.Text = CStr(txtWidth.Tag)
            txtTopM.Text = CStr(txtTopM.Tag)
            txtBottomM.Text = CStr(txtBottomM.Tag)
            txtLeftM.Text = CStr(txtLeftM.Tag)
            txtRightM.Text = CStr(txtRightM.Tag)
            txtLaylbl.Text = CStr(txtLaylbl.Tag)

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "CancelData")
        End Try
    End Sub
    Public Sub ViewLayoutMargin()

        Try
            If BDocument.GetData(intLayoutId).Rows.Count > 0 Then
                With BDocument.GetData(intLayoutId).Rows(0)
                    txtHeight.Text = .Item("Height")
                    txtHeight.Tag = .Item("Height")
                    txtWidth.Text = .Item("Width")
                    txtWidth.Tag = .Item("Width")
                    txtTopM.Text = .Item("TopM")
                    txtTopM.Tag = .Item("TopM")
                    txtBottomM.Text = .Item("BottomM")
                    txtBottomM.Tag = .Item("BottomM")
                    txtLeftM.Text = .Item("LeftM")
                    txtLeftM.Tag = .Item("LeftM")
                    txtRightM.Text = .Item("RightM")
                    txtRightM.Tag = .Item("RightM")
                    txtLaylbl.Text = .Item("Layoutlbl")
                    txtLaylbl.Tag = .Item("Layoutlbl")
                End With
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ViewLayoutMargin")
        End Try
    End Sub
#End Region 'End User Define

#Region "Events"

    Private Sub btnSetValue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSetValue.Click
        Try
            SetValues()
            If ValidateData() Then
                If btnSetValue.Text = "&Save" Then
                    If BDocument.SaveData(Cdoc, "S") Then BCommon.ShowMsgError(3)
                    'Divya@01/15/2008 : Added following IF condition
                    If BCommon.ShowMsgError(10330, "YN") = Windows.Forms.DialogResult.Yes Then
                        Call PrintBill()
                    End If
                Else
                    If BDocument.SaveData(Cdoc, "U") Then Call BCommon.ShowMsgError(5)
                    'Divya@01/15/2008 : Added following IF condition
                    If BCommon.ShowMsgError(10330, "YN") = Windows.Forms.DialogResult.Yes Then
                        Call PrintBill()
                    End If
                    btnSetValue.Text = "&Save"
                End If
                txtLaylbl.Tag = ""
                Call ClearData()
                Call CancelData()
                txtLaylbl.Focus()
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnSetValue_Click")
        End Try
    End Sub
    'Divya@16/01/2008
    Private Sub btnPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreview.Click
        'Call SetValues()
        If ValidateData() Then
            Call Preview()
        End If
    End Sub

    Private Sub PrintDoc_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDoc.PrintPage

        Dim fmt As New StringFormat(StringFormatFlags.LineLimit)
        Dim TextSize As New System.Drawing.SizeF
        Y = PrintDoc.DefaultPageSettings.Margins.Top + 10
        curY = PrintDoc.DefaultPageSettings.Margins.Top + 10
        Dim count As Integer = 0
        Dim curRowHeight As Integer
        Dim FootStart As Integer

        Try
            Call SetPageHeader(e) 'Setting the Header
            FootStart = PHi - GetFooterHeight(e) - PrintDoc.DefaultPageSettings.Margins.Bottom


            TextSize = e.Graphics.MeasureString("XXXXXXXXXX", tableFont, 198)
            curRowHeight = TextSize.Height

            e.Graphics.DrawString("XXXXXXXXXX", tableFont, Brushes.Black, DrawRectangelF(X1, curY, (wi * 8) - 2, curRowHeight), fmt)
            e.Graphics.DrawString("XXXX", tableFont, Brushes.Black, DrawRectangelF(X1 + (wi * 8), curY, (wi * 10) - 2, curRowHeight), fmt)
            e.Graphics.DrawString("XXXX", tableFont, Brushes.Black, DrawRectangelF(X1 + (wi * 10), curY, (wi * 12) - 2, curRowHeight), fmt)
            e.Graphics.DrawString("XXXX", tableFont, Brushes.Black, DrawRectangelF(X1 + (wi * 12), curY, (wi * 15) - 2, curRowHeight), fmt)
            e.Graphics.DrawString("XXXX", tableFont, Brushes.Black, DrawRectangelF(X1 + (wi * 15), curY, (wi * 17) - 2, curRowHeight), fmt)
            e.Graphics.DrawString("XXXX", tableFont, Brushes.Black, DrawRectangelF(X1 + (wi * 17), curY, (wi * 3), curRowHeight), fmt)
            count = count + 1
            curY = curY + curRowHeight
            e.HasMorePages = False
            curY = FootStart
            Call SetPageFooter(e)      'Setting the Footer When Record Available in One Page
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "PrintDocument1_PrintPage")
        End Try
    End Sub
    

    Private Sub frmDocuments_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        If isChange() Then
             Select Case BCommon.ShowMsgError(10081, "YNC")
                Case Windows.Forms.DialogResult.Yes
                    SetValues()
                    If ValidateData() Then
                        If btnSetValue.Text = "&Save" Then
                            'If BDocument.SaveData(Cdoc, "S") Then
                            '    PrintBill()
                            'End If
                            BDocument.SaveData(Cdoc, "S")
                            BCommon.ShowMsgError(3)
                        Else
                            'If BDocument.SaveData(Cdoc, "U") Then
                            '    PrintBill()
                            'End If
                            BDocument.SaveData(Cdoc, "U")
                            BCommon.ShowMsgError(5)
                        End If
                    Else
                        e.Cancel = True
                    End If
                Case Windows.Forms.DialogResult.Cancel
                    e.Cancel = True
                Case Windows.Forms.DialogResult.No
            End Select
        End If
    End Sub
    Public Function isChange() As Boolean
        Try
            If Trim(txtHeight.Text) <> txtHeight.Tag _
            Or Trim(txtWidth.Text) <> txtWidth.Tag Or Trim(txtTopM.Text) <> txtTopM.Tag _
            Or Trim(txtBottomM.Text) <> txtBottomM.Tag Or Trim(txtLeftM.Text) <> txtLeftM.Tag _
            Or Trim(txtRightM.Text) <> txtRightM.Tag Or Trim(txtLaylbl.Text) <> txtLaylbl.Tag Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "isChange")
        End Try
    End Function

    Private Sub frmDocuments_HelpButtonClicked(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.HelpButtonClicked
        Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Document Layout")
        e.Cancel = True
    End Sub

    Private Sub frmDocuments_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyCode = Keys.F1 Then
            e.SuppressKeyPress = True
            Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Document Layout")
        End If
    End Sub

    Private Sub frmDocuments_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Call BCommon.SetFormStandards(Me)
            Call ClearData()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "frmDocuments_Load")
        End Try
    End Sub

    Private Sub txtHeight_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtHeight.KeyDown
        Try
            If e.KeyCode = Keys.Enter Then txtTopM.Focus()
            If BCommon.ValidateText(txtHeight.Text, e.KeyCode, "NUM", False) = False Then
                e.SuppressKeyPress = True
            ElseIf e.Shift = True Then
                e.SuppressKeyPress = True
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtHeight_KeyDown")
        End Try
    End Sub

    Private Sub txtWidth_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtWidth.KeyDown
        Try
            If e.KeyCode = Keys.Enter Then txtLeftM.Focus()
            If BCommon.ValidateText(txtWidth.Text, e.KeyCode, "NUM", False) = False Then
                e.SuppressKeyPress = True
            ElseIf e.Shift = True Then
                e.SuppressKeyPress = True
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtWidth_KeyDown")
        End Try
    End Sub

    Private Sub txtLeftM_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtLeftM.KeyDown
        Try
            If e.KeyCode = Keys.Enter Then txtRightM.Focus()
            If BCommon.ValidateText(txtLeftM.Text, e.KeyCode, "NUM", False) = False Then
                e.SuppressKeyPress = True
            ElseIf e.Shift = True Then
                e.SuppressKeyPress = True
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtLeftM_KeyDown")
        End Try
    End Sub

    Private Sub txtRightM_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtRightM.KeyDown
        Try
            If e.KeyCode = Keys.Enter Then btnPreview.Focus()
            If BCommon.ValidateText(txtRightM.Text, e.KeyCode, "NUM", False) = False Then
                e.SuppressKeyPress = True
            ElseIf e.Shift = True Then
                e.SuppressKeyPress = True
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtRightM_KeyDown")
        End Try
    End Sub

    Private Sub txtTopM_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTopM.KeyDown
        Try
            If e.KeyCode = Keys.Enter Then txtBottomM.Focus()
            If BCommon.ValidateText(txtTopM.Text, e.KeyCode, "NUM", False) = False Then
                e.SuppressKeyPress = True
            ElseIf e.Shift = True Then
                e.SuppressKeyPress = True
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtTopM_KeyDown")
        End Try
    End Sub

    Private Sub txtBottomM_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtBottomM.KeyDown
        Try
            If e.KeyCode = Keys.Enter Then txtWidth.Focus()
            If BCommon.ValidateText(txtBottomM.Text, e.KeyCode, "NUM", False) = False Then
                e.SuppressKeyPress = True
            ElseIf e.Shift = True Then
                e.SuppressKeyPress = True
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtBottomM_KeyDown")
        End Try
    End Sub

    Private Sub txtHeight_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtHeight.LostFocus
        Try
            txtHeight.Text = checkTextBox(txtHeight.Text)
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtHeight_LostFocus")
        End Try
    End Sub

    Private Sub txtWidth_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtWidth.LostFocus
        Try
            txtWidth.Text = checkTextBox(txtWidth.Text)
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtWidth_LostFocus")
        End Try
    End Sub

    Private Sub txtLeftM_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtLeftM.LostFocus
        Try
            txtLeftM.Text = checkTextBox(txtLeftM.Text)
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtLeftM_LostFocus")
        End Try
    End Sub

    Private Sub txtRightM_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtRightM.LostFocus
        Try
            txtRightM.Text = checkTextBox(txtRightM.Text)
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtRightM_LostFocus")
        End Try
    End Sub

    Private Sub txtTopM_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTopM.LostFocus
        Try
            txtTopM.Text = checkTextBox(txtTopM.Text)
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtTopM_LostFocus")
        End Try
    End Sub

    Private Sub txtBottomM_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtBottomM.LostFocus
        Try
            txtBottomM.Text = checkTextBox(txtBottomM.Text)
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtBottomM_LostFocus")
        End Try
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Try
            Me.Close()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnClose_Click")
        End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        txtLaylbl.Tag = ""
        Call ClearData()
        Call CancelData()
        txtLaylbl.Focus()
        btnSetValue.Text = "&Save"
    End Sub

    Private Sub txtLaylbl_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtLaylbl.KeyDown
        If e.Alt Then Exit Sub
        If e.KeyCode = Keys.Enter Then txtHeight.Focus()
        'Divya@28/01/2008
        If e.Shift = True Then
            If (e.KeyCode >= 48 And e.KeyCode <= 48 + 10) Or (e.KeyCode >= 96 And e.KeyCode <= 96 + 10) Then
                e.SuppressKeyPress = True : Exit Sub
            End If
        End If
        If BCommon.ValidateText(e.KeyCode, "ALFTEXT", False) = False Then
            e.SuppressKeyPress = True
        End If
        'END
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Try
            Dim form As New frmMasterBrowse
            form.strSearch = "Layout"
            'Rahul_ITS@20072612
            '<<START
            form.StartPosition = FormStartPosition.Manual
            form.Height = MDIPharmacy.MdiScaleHeight
            form.Width = MDIPharmacy.MdiScaleWidth
            Dim pt As New Point
            pt.Y = ((MDIPharmacy.MdiScaleHeight - form.Height) / 2) + MDIPharmacy.pnlHeading.Height + MDIPharmacy.MenuStrip.Height
            pt.X = (MDIPharmacy.MdiScaleWidth - form.Width) / 2
            form.Location = pt
            form.ShowInTaskbar = False
            '>>END
            form.ShowDialog()
            Cdoc.LayoutId = form.strRecordNo
            intLayoutId = Cdoc.LayoutId

            If intLayoutId <> 0 Then
                Call ViewLayoutMargin() : btnSetValue.Text = "&Update"
            End If

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnSearch_Click")
        End Try
    End Sub
    Private Sub SetPageHeader(ByVal e As System.Drawing.Printing.PrintPageEventArgs)
        Dim X As Integer
        Dim TextSize As New System.Drawing.SizeF
        Dim XStart As Integer
        Dim dtFirm As New DataTable

        Try
            dtFirm = BCommon.FirmInfo()
            With PrintDoc.DefaultPageSettings
                X = .PaperSize.Width - .Margins.Left - .Margins.Right
            End With

            'HEADER STATIC
            TextSize = e.Graphics.MeasureString("Date: " & Format(Now, "dd/MM/yy"), titlefont)
            XStart = X - (TextSize.Width + 10)
            'TextSize = e.Graphics.MeasureString("FIRMNAME", headfont)
            TextSize = e.Graphics.MeasureString(Trim(dtFirm.Rows(0).Item("FirmName").ToString()), headfont)
            'e.Graphics.DrawString("FIRMNAME", headfont, Brushes.Black, DrawRectangelF(X1, curY, XStart, TextSize.Height))
            e.Graphics.DrawString(Trim(dtFirm.Rows(0).Item("FirmName").ToString()), headfont, Brushes.Black, DrawRectangelF(X1, curY, XStart, TextSize.Height))
            TextSize = e.Graphics.MeasureString("Date: " & Format(Now, "dd/MM/yy"), titlefont)
            e.Graphics.DrawString("Date: " & Format(Now, "dd/MM/yy"), titlefont, Brushes.Black, DrawRectangelF(XStart + X1, curY, TextSize.Width + 10, TextSize.Height))
            'Text Size Set to Height of FirmName for Y Position.
            'TextSize = e.Graphics.MeasureString("FIRMNAME", headfont)
            TextSize = e.Graphics.MeasureString(Trim(dtFirm.Rows(0).Item("FirmName").ToString()), headfont)
            curY = curY + TextSize.Height

            'HEADER LINE 1  
            e.Graphics.DrawString(Trim(dtFirm.Rows(0).Item("addDesc1").ToString()) & "," & Trim(dtFirm.Rows(0).Item("Description").ToString()) & "-" & Trim(dtFirm.Rows(0).Item("PinCode").ToString()) & ", Tel: " & Trim(dtFirm.Rows(0).Item("phone1").ToString()), titlefont, Brushes.Black, X1, curY)
            TextSize = e.Graphics.MeasureString(Trim(dtFirm.Rows(0).Item("addDesc1").ToString()) & "," & Trim(dtFirm.Rows(0).Item("Description").ToString()) & "-" & Trim(dtFirm.Rows(0).Item("PinCode").ToString()) & ", Tel: " & Trim(dtFirm.Rows(0).Item("phone1").ToString()), titlefont)
            curY = curY + TextSize.Height

            'HEADER LINE 2  

            TextSize = e.Graphics.MeasureString("Addr : xxxx", titlefont)
            XStart = X - (TextSize.Width + 10)
            e.Graphics.DrawString("Patient : xxxxxxxx", titlefont, Brushes.Black, DrawRectangelF(X1, curY, XStart, TextSize.Height))
            e.Graphics.DrawString("Addr : xxxx", titlefont, Brushes.Black, DrawRectangelF(XStart + X1, curY, TextSize.Width, TextSize.Height))
            curY = curY + TextSize.Height

            'HEADER LINE 3  
            TextSize = e.Graphics.MeasureString("Bill No: xxxxxxx", titlefont)
            XStart = X - (TextSize.Width + 10)
            e.Graphics.DrawString("Doctor : xxxxxxxx", titlefont, Brushes.Black, DrawRectangelF(X1, curY, XStart, TextSize.Height))
            e.Graphics.DrawString("Bill No: xxxxxxx", titlefont, Brushes.Black, DrawRectangelF(XStart + X1, curY, TextSize.Width, TextSize.Height))
            TextSize = e.Graphics.MeasureString("Doct xxxxxxx: ", titlefont)
            curY = curY + TextSize.Height

            'e.Graphics.DrawString("Doct : xxxxxxxx", titlefont, Brushes.Black, X1, curY)
            'TextSize = e.Graphics.MeasureString("Doct xxxxxxx: ", titlefont)
            'curY = curY + TextSize.Height


            With PrintDoc.DefaultPageSettings
                e.Graphics.DrawLine(Pens.Black, .Margins.Left, curY, .PaperSize.Width - .Margins.Right, curY)
                'X = .PaperSize.Width - .Margins.Left - .Margins.Right
                wi = X / 20
            End With
            'DESC  +  COMP  +  EXP  + BATCH + QTY  + AMOUNT 
            '  8   +    2   +   2   +   3   +   2  +   2
            'Caption of the Print Bill
            '=========================
            e.Graphics.DrawString("Description", tabletitlefont, Brushes.Black, X1, curY)
            e.Graphics.DrawString("Comp", tabletitlefont, Brushes.Black, X1 + (wi * 8), curY)
            e.Graphics.DrawString("Exp", tabletitlefont, Brushes.Black, X1 + (wi * 10), curY)
            e.Graphics.DrawString("BatchNo", tabletitlefont, Brushes.Black, X1 + (wi * 12), curY)
            e.Graphics.DrawString("Qty", tabletitlefont, Brushes.Black, X1 + (wi * 15), curY)
            e.Graphics.DrawString("Amount", tabletitlefont, Brushes.Black, X1 + (wi * 17), curY)
            TextSize = e.Graphics.MeasureString("Description", tabletitlefont)
            curY = curY + TextSize.Height
            With PrintDoc.DefaultPageSettings
                e.Graphics.DrawLine(Pens.Black, .Margins.Left, curY, .PaperSize.Width - .Margins.Right, curY)
                'FinalSize = Y + 110 + 20
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetPageHeader")
        End Try
    End Sub

    Private Sub SetPageFooter(ByVal e As System.Drawing.Printing.PrintPageEventArgs)
        Dim TextSize As New System.Drawing.SizeF
        Dim FooterRowHieght As Integer
        TextSize = e.Graphics.MeasureString("Pharmacist", titlefont)
        FooterRowHieght = TextSize.Height
        Dim XStart As Integer
        Dim PWidth As Integer ' Page Width 
        Dim tempstr As String
        Try
            With PrintDoc.DefaultPageSettings
                e.Graphics.DrawLine(Pens.Black, .Margins.Left, curY, .PaperSize.Width - .Margins.Right, curY)
                PWidth = .PaperSize.Width - .Margins.Right - .Margins.Left
            End With

            If e.HasMorePages = False Then
                TextSize = e.Graphics.MeasureString("Net Amt : Rs. 0.00", titlefont)
                XStart = PWidth - (TextSize.Width + 10)
                e.Graphics.DrawString("Total :" & ConfigurationManager.AppSettings("Currency") & " 0.00", titlefont, Brushes.Black, X1, curY)

                e.Graphics.DrawString("Disc : " & ConfigurationManager.AppSettings("Currency") & " 0.00 ", titlefont, Brushes.Black, (XStart / 2), curY)

                e.Graphics.DrawString("Net Amt : " & ConfigurationManager.AppSettings("Currency") & " 0.00", titlefont, Brushes.Black, XStart, curY)
                curY = curY + FooterRowHieght

                e.Graphics.DrawString("Paid : " & ConfigurationManager.AppSettings("Currency") & " 0.00", titlefont, Brushes.Black, (XStart / 2), curY)

                TextSize = e.Graphics.MeasureString("Bal Amt : " & ConfigurationManager.AppSettings("Currency") & " 0.00", titlefont)
                XStart = PWidth - (TextSize.Width + 10)

                e.Graphics.DrawString("Bal Amt : " & ConfigurationManager.AppSettings("Currency") & " 0.00", titlefont, Brushes.Black, XStart, curY)
                curY = curY + FooterRowHieght
            End If

            With PrintDoc.DefaultPageSettings
                e.Graphics.DrawLine(Pens.Black, .Margins.Left, curY, .PaperSize.Width - .Margins.Right, curY)
            End With

            '*********FOOTER SECTION
            'FOOTER LINE 1
            tempstr = "For " & BCommon.GetFirmFullName
            TextSize = e.Graphics.MeasureString(tempstr, titlefont)
            XStart = PWidth - (TextSize.Width + 10)
            'e.Graphics.DrawString("xxxxx", titlefont, Brushes.Black, DrawRectangelF(X1, curY, XStart - 10, FooterRowHieght))
            e.Graphics.DrawString(Foot1, titlefont, Brushes.Black, DrawRectangelF(X1, curY, XStart - 10, FooterRowHieght))
            e.Graphics.DrawString(tempstr, titlefont, Brushes.Black, DrawRectangelF(XStart, curY, TextSize.Width, FooterRowHieght))
            curY = curY + FooterRowHieght
            'FOOTER LINE 2
            'e.Graphics.DrawString("xxxxx", titlefont, Brushes.Black, X1, curY)
            e.Graphics.DrawString(Foot2, titlefont, Brushes.Black, X1, curY)
            curY = curY + FooterRowHieght
            'FOOTER LINE 3
            tempstr = "Pharmacist"
            TextSize = e.Graphics.MeasureString(tempstr, titlefont)
            XStart = PWidth - (TextSize.Width + 10)
            'e.Graphics.DrawString("xxxxx12", titlefont, Brushes.Black, DrawRectangelF(X1, curY, XStart - 10, FooterRowHieght))
            e.Graphics.DrawString(Foot3, titlefont, Brushes.Black, DrawRectangelF(X1, curY, XStart - 10, FooterRowHieght))
            'e.Graphics.DrawRectangle(Pens.Black, XStart, curY, XStart, FooterRowHieght)
            e.Graphics.DrawString(tempstr, titlefont, Brushes.Black, DrawRectangelF(XStart, curY, XStart, FooterRowHieght))
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetPageFooter")
        End Try
    End Sub
    Private Function GetFooterHeight(ByVal e As System.Drawing.Printing.PrintPageEventArgs) As Integer
        Dim TextSize As New System.Drawing.SizeF
        Dim TotFooterHieght As Integer
        TextSize = e.Graphics.MeasureString("Pharmacist", titlefont)
        '3 Footer Line And One Total Amount Line 
        TotFooterHieght = TextSize.Height * 5
        Return TotFooterHieght
    End Function
    Private Function DrawRectangelF(ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer) As RectangleF
        Try
            Dim drawRect As New RectangleF(x, y, width, height)
            Return drawRect
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "DrawRectangelF")
        End Try
    End Function
#End Region 'End Events
End Class